package com.tencent.order.service.impl;

import com.tencent.basic.service.serviceimpl.BaseServiceImpl;
import com.tencent.basic.util.LoginContext;
import com.tencent.basic.util.PageList;
import com.tencent.order.domain.OrderPetAcquisition;
import com.tencent.order.dto.OrderDto;
import com.tencent.order.mapper.OrderPetAcquisitionMapper;
import com.tencent.order.query.OrderPetAcquisitionQuery;
import com.tencent.order.service.IOrderPetAcquisitionService;
import com.tencent.org.domain.Emp;
import com.tencent.org.domain.Shop;
import com.tencent.org.mapper.EmpMapper;
import com.tencent.org.mapper.ShopMapper;
import com.tencent.pet.domain.Pet;
import com.tencent.pet.domain.SearchMasterMsg;
import com.tencent.pet.mapper.PetMapper;
import com.tencent.pet.mapper.SearchMasterMsgMapper;
import com.tencent.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmpMapper empMapper;
    
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    @Transactional
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Emp emp = empMapper.findByLogininfoId(logininfo.getId());
        if(emp.getShop_id() != null){//店铺管理员【店长 or 店员】
            query.setShopId(emp.getShop_id());
            Shop shop = shopMapper.findById(emp.getShop_id());
            //不等于店员，等于就是店长
            if(shop.getAdminId()!=emp.getId()){
                query.setEmpId(emp.getId());
            }
        }

        return super.queryPage(query);
    }

    @Override
    @Transactional
    public void cancel(Long id) { //订单id
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(id);
        //1.shopId置空
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //2.删除宠物信息 - 订单取消了，宠物就没有接回来。之前已经将宠物录入系统了
        petMapper.del(order.getPetId());

        //3.改状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }

    @Override
    @Transactional
    public void confirm(OrderDto orderDto) {
        //1.改订单状态 0待处理 - 1已处理
        //2.修改订单的支付方式和金
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(orderDto.getId());
        order.setState(1);
        order.setPaytype(orderDto.getPayType());
        order.setPrice(orderDto.getMoney());
        orderPetAcquisitionMapper.update(order);


        //3.改寻主消息状态 1待处理 -2已完成
        SearchMasterMsg msg = searchMasterMsgMapper.findById(order.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

        //4.修改pet的成本价
        Pet pet = petMapper.findById(order.getPetId());
        pet.setCostprice(orderDto.getMoney());
        petMapper.update(pet);
    }
}
